<template>
  <div class="mod-user">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-select
          v-model="dataForm.value"
          placeholder="请选择"
          @visible-change="checked($event)"
          @change="change($event)"
        >
                  <el-option label="全选" value="全选"></el-option>
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!-- <el-button type="primary"
          ><i class="el-icon-printer"></i>打印</el-button
        > -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        prop="materialName"
        header-align="center"
        align="center"
        label="名称"
      >
      </el-table-column>
      <el-table-column
        prop="materialModel"
        header-align="center"
        align="center"
        label="型号"
      >
      </el-table-column>
      <el-table-column
        prop="materialOther"
        header-align="center"
        align="center"
        label="扩展信息"
      >
      </el-table-column>
      <el-table-column
        prop="materialUnit"
        header-align="center"
        align="center"
        width="180"
        label="单位"
      >
      </el-table-column>
      <el-table-column
        prop="basicInNumber"
        header-align="center"
        align="center"
        label="入库数量"
      >
      </el-table-column>
      <el-table-column
        prop="basicOutNumber"
        header-align="center"
        align="center"
        label="出库数量"
      >
      </el-table-column>
      <el-table-column
        prop="basicNumber"
        header-align="center"
        align="center"
        label="库存数量"
      >
      </el-table-column>
      <el-table-column
        prop="safetystock"
        header-align="center"
        align="center"
        label="安全库存"
      >
      </el-table-column>
      <el-table-column
        prop="basicLinjieNumber"
        header-align="center"
        align="center"
        label="临界库存量"
      >
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./stockwarning-add-or-update";
export default {
  data() {
    return {
      dataForm: {
        value: "全选",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      options: [],
      value: "",
    };
  },
  components: {
    AddOrUpdate,
  },
  activated() {
    this.getDataList();
  },
  methods: {
    change(id) {
      // console.log(id)
      this.dataForm.id = id;
    },
    checked(e) {
      if (e) {
      }
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      if(this.dataForm.value== "全选"){
        this.dataForm.id=""
      }
      this.$http({
        url: this.$http.adornUrl("/depot/getAllList"),
        method: "get",
      })
        .then(({ data }) => {
          console.log(data.data);
          if (data && data.code === 0) {
            this.options = data.data;
            this.totalPage = data.data.totalCount;
            // console.log(this.options[0].name);
            // this.dataForm.value = this.options[0].name;
            // this.dataForm.id = this.options[0].id;
          }
        })
        .then((data) => {
          this.$http({
            url: this.$http.adornUrl("/depotItem/findStockWarningCount"),
            method: "get",
            params: this.$http.adornParams({
              currentPage: this.pageIndex,
              pageSize: this.pageSize,
              projectId:this.dataForm.id,
            }),
          }).then(({ data }) => {
            console.log(data);
            if (data && data.code === 0) {
              this.dataList = data.data.rows;
              this.totalPage = data.data.rows.length;
            } else {
              this.dataList = [];
              this.totalPage = 0;
            }
            this.dataListLoading = false;
          });
        });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      // console.log(id)
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var userIds = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.userId;
          });
      this.$confirm(
        `确定对[id=${userIds.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/sys/user/delete"),
            method: "post",
            data: this.$http.adornData(userIds, false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },

    // 重置
    resetDataList() {
      this.dataForm.value = "仓库2";
    },
  },
};
</script>
